// 递归 - 四星难度
function findMax(str) {
  const charObj = {}
  let index = 0
  let maxNum = 0
  let maxChar = ''
  for (let i = 0; i < str.length; i ++) {
    const c = str.charAt(i)
    !charObj[c] ? charObj[c] = 1 : charObj[c] += 1
    const num = charObj[c]
    if (num > maxNum) {
      index = i
      maxNum = num
      maxChar = c
    }
  }
  console.log('----------')
  console.log('str=', str)
  console.log('maxChar=', maxChar, ' maxNum=', maxNum)
  return {charObj, maxChar, maxNum}
}

const app = new Vue({
  el: '#demo',
  data: {
    str: 'sfsfwefwefwfwefefwefwefppfafcf5f22112fop',
    maxChar: 0,
    maxNum: '',
    obj: {}
  },
  created() {
    this.touchResult()
  },
  methods: {
    reloadData() {
      const charStr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
      const charLenght = charStr.length
      let joinStr = ''
      for (let i = 0; i < 20; i ++) {
        const index = Math.floor(Math.random() * charLenght + 1)
        joinStr += charStr.charAt(index)
      }
      this.str = joinStr
      this.touchResult()
    },
    touchResult() {
      const obj = findMax(this.str)
      console.log('obj=', obj)
      this.obj = obj.charObj
      this.maxChar = obj.maxChar
      this.maxNum = obj.maxNum
    }
  }
})